import { api } from '@/utils/request'
import type { 
  Role, 
  RolePageParams,
  PageResult,
  ApiResponse 
} from '@/types'

export class RoleService {
  /**
   * 分页查询角色列表
   */
  static async getRolePage(params: RolePageParams): Promise<PageResult<Role>> {
    const response = await api.post<PageResult<Role>>('/auth/api-admin/authRole/page', params)
    return response.data
  }

  /**
   * 获取角色详情
   */
  static async getRoleDetail(id: number): Promise<Role> {
    const response = await api.get<Role>(`/auth/api-admin/authRole/get/${id}`)
    return response.data
  }

  /**
   * 新增角色
   */
  static async addRole(roleData: Partial<Role> & { permissionIds?: number[] }): Promise<boolean> {
    const response = await api.post<boolean>('/auth/api-admin/authRole/create', roleData)
    return response.data
  }

  /**
   * 更新角色
   */
  static async updateRole(roleData: Partial<Role> & { permissionIds?: number[] }): Promise<boolean> {
    const response = await api.post<boolean>('/auth/api-admin/authRole/update', roleData)
    return response.data
  }

  /**
   * 删除角色
   */
  static async deleteRole(id: number): Promise<boolean> {
    const response = await api.delete<boolean>(`/auth/api-admin/authRole/delete/${id}`)
    return response.data
  }

  /**
   * 获取角色列表（不分页）
   */
  static async getRoleList(): Promise<Role[]> {
    const response = await api.get<Role[]>('/auth/api-admin/authRole/list')
    return response.data
  }

  /**
   * 为角色分配权限（已整合到新增和更新接口中）
   */
  static async assignPermissions(roleId: number, permissionIds: number[]): Promise<boolean> {
    const response = await api.post<boolean>('/api-admin/authRole/assignPermissions', {
      roleId,
      permissionIds
    })
    return response.data
  }
}

// 默认导出单例实例
export const roleService = RoleService 